﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class test_GetListImgFromHTML : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string strHtml = OsPortal.oRegexHelper.LoadHTML("http://www.ongon.info/2014/01/banh-hanh-nhan-co-ien-cua-phap.html");
        GridView1.DataSource = GetImagesInHTMLString(strHtml);
        GridView1.DataBind();
        Response.Write(GetFileSize(GetImagesInHTMLString(strHtml)[0].ToString()));
    }

    public static List<Uri> GetImagesInHTMLString(string htmlString)
    {
        List<Uri> links = new List<Uri>();
        string regexImgSrc = @"<img[^>]*?src\s*=\s*[""']?([^'"" >]+?)[ '""][^>]*?>";
        MatchCollection matchesImgSrc = Regex.Matches(htmlString, regexImgSrc, RegexOptions.IgnoreCase | RegexOptions.Singleline);
        foreach (Match m in matchesImgSrc)
        {
            string href = m.Groups[1].Value;
            string strType = href.Substring(href.Length - 3, 3);
            if (strType == "jpg" || strType == "png" || strType == "gif" || strType == "JPG")
            {
                if (GetFileSize(href) > 100000)
                    links.Add(new Uri(href));
            }
            //else strImg = "/Images/System/link.png";

        }
        return links;
    }

    public static int GetFileSize(string fileUrl)
    {
        var request = System.Net.HttpWebRequest.Create(fileUrl);

        request.Method = "HEAD";

        using (var response = request.GetResponse())
        {
            int contentLength = 0;
            int.TryParse(response.Headers.Get("Content-Length"),
                    out contentLength);

            return contentLength;
        }
    }
}